-module(user_service).
-include("rec_dec.hrl").
-include_lib("stdlib/include/qlc.hrl").
-export([addchange/1,get/0,del/2,start/0,get/1,delete/1]).
-define(TAB    , user_rec).
-define(U_V_REL_TAB, user_vendor_rec).
-define(LOGIN_TAB, login_rec). 


addchange(Rec) when is_record(Rec,?TAB) ->
	case Rec#?TAB.id of
	"" ->
		add(Rec);
	<<"">> ->
		add(Rec);
	_ ->
		modify(Rec)
	end;
addchange(Rec) when is_record(Rec,v_register_rec) ->
	modify(#user_rec{
		id = Rec#v_register_rec.id,
		name = Rec#v_register_rec.name,
		phone = Rec#v_register_rec.mobile,
		login_name = Rec#v_register_rec.mobile,
		password = Rec#v_register_rec.password,
		right = admin,
		register_id = Rec#v_register_rec.id
		});
addchange(Log) when is_record(Log,?LOGIN_TAB) ->
	db_mgr:write(?LOGIN_TAB,Log);
	
addchange(Rec) when is_record(Rec,?U_V_REL_TAB) ->
	db_mgr:write(?U_V_REL_TAB,Rec).
	
add(Rec) when is_record(Rec,?TAB) ->
	Id = gen_server:call(couch_uuids,create),
	db_mgr:write(?TAB,Rec#?TAB{id=Id}).
	

modify(Rec) when is_record(Rec,?TAB) ->
	db_mgr:write(?TAB,Rec).
	
get() -> %% get all vendor info
	db_mgr:get(?TAB).

get({id,Id}) when is_binary(Id) ->
	case db_mgr:get(?TAB,Id) of
	[Res|_] ->
		[Res];
	_ ->
		[]
	end;
get({login_rec,U_Id}) ->
	do(qlc:q([X|| X <- mnesia:table(?LOGIN_TAB), X#login_rec.user_id =:= U_Id]));
	
get({register_id,Id}) ->
	do(qlc:q([X|| X <- mnesia:table(?TAB), X#user_rec.register_id =:= Id]));
	%%db_mgr:get(qlc_st,Qry).

get({name,Name}) ->
	do(
		qlc:q([X||X <- mnesia:table(?TAB), X#user_rec.login_name =:= Name])
	);
get({phone,Num}) ->
	do(
		qlc:q([X||X <- mnesia:table(?TAB), X#user_rec.phone =:= Num])
	);


get({vendor_id,Vid}) ->
	case vendor_service:get(Vid) of
	#vendor_rec{name="missed"} ->
		[];
	V ->
		AllUsers = do(qlc:q([X||X <- mnesia:table(?TAB), X#user_rec.register_id =:= V#vendor_rec.register_id,
			X#user_rec.right =:= operator])),
		AssignedUsers = do(qlc:q([{X#user_rec.id,X,true} ||
												 X <- mnesia:table(?TAB),Y <-mnesia:table(?U_V_REL_TAB), 
												Vid =:= Y#user_vendor_rec.v_id,
												X#user_rec.id =:= Y#user_vendor_rec.u_id])),
		lists:map(
			fun(E) ->
				case lists:keyfind(E#user_rec.id,1,AssignedUsers) of
				{_,E,S} ->
					{E,S};
				_ ->
					{E,false}
				end
			end,
			AllUsers)
	end.

start() ->
	case db_mgr:check_table(?TAB) of
	no_found ->
		db_mgr:create_table(?TAB,record_info(fields,?TAB));
	_ ->
		ok
	end,
	case db_mgr:check_table(?U_V_REL_TAB) of
	no_found ->
		db_mgr:create_table(?U_V_REL_TAB,record_info(fields,?U_V_REL_TAB),[{type,bag}]);
	_ ->
		ok
	end,
	case db_mgr:check_table(?LOGIN_TAB) of
	no_found ->
		db_mgr:create_table(?LOGIN_TAB,record_info(fields,?LOGIN_TAB));
	_ ->
		ok
	end.

del(user_vendor_rel,{vendor_id,Vid}) ->
	F = fun() -> 
			list:foreach(fun(A) ->
							mnesia:delete_object(?U_V_REL_TAB,A,write)
						end,
						do(qlc:q([X || X <- mnesia:table(?U_V_REL_TAB), Vid =:= X#user_vendor_rec.v_id]))
				)
		end,
	mnesia:transaction(F);

del(user_vendor_rel,{user_id,Uid}) ->
	F = fun() -> 
			list:foreach(fun(A) ->
							mnesia:delete_object(?U_V_REL_TAB,A,write)
						end,
						do(qlc:q([X || X <- mnesia:table(?U_V_REL_TAB), Uid =:= X#user_vendor_rec.u_id]))
				)
		end,
	mnesia:transaction(F);


del(user_vendor_rel,{Vid,Uid}) ->
	F = fun() -> lists:foreach(
						fun(A) ->
							mnesia:delete_object(?U_V_REL_TAB,A,write) 
						end,
						do(qlc:q([X || X <- mnesia:table(?U_V_REL_TAB), Vid =:= X#user_vendor_rec.v_id, Uid =:= X#user_vendor_rec.u_id])))
					end,
    mnesia:transaction(F);
	
del(user,ID) -> 
	db_mgr:del(?TAB,ID).
delete(Id) ->
	del(user,Id).	
do(Q) ->
	F = fun() -> qlc:e(Q) end,
	{atomic, Val} = mnesia:transaction(F),
	Val.